import QRCode  from '../../../utils/weapp-qrcode.js';
var qrcode;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    name: '',
    beginTime: '',
    endTime: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let _id = options.id;
    let beginTime = options.beginTime;
    let endTime = options.endTime;
    let name = options.name;
    this.setData({
      name: name,
      beginTime: beginTime,
      endTime: endTime
    })
    qrcode = new QRCode('canvas', {
      // usingIn: this,
      text: _id,
      padding: 12,
      width: 250,
      height: 250,
      colorDark: "#1CA4FC",
      colorLight: "white",
      correctLevel: QRCode.CorrectLevel.H,
      callback: (res) => {
        // 生成二维码的临时文件
        console.log(res.path)
      }
    });
  },

  // 长按保存
  save: function () {
    let that = this
    wx.showActionSheet({
      itemList: ['保存图片'],
      success: function (res) {
        console.log(res.tapIndex)
        if (res.tapIndex == 0) {
          wx.getSetting({
            success(res) {
              //没有权限，发起授权
              if (!res.authSetting['scope.writePhotosAlbum']) {
                wx.authorize({
                  scope: 'scope.writePhotosAlbum',
                  success() {//用户允许授权，保存图片到相册
                    that.savePhoto();
                  },
                  fail() {//用户点击拒绝授权，跳转到设置页，引导用户授权
                    wx.openSetting({
                      success() {
                        wx.authorize({
                          scope: 'scope.writePhotosAlbum',
                          success() {
                            that.savePhoto();
                          }
                        })
                      }
                    })
                  }
                })
              } else {//用户已授权，保存到相册
                that.savePhoto()
              }
            }
          })
        }
      }
    })
  },
  savePhoto(){
   console.log('qrcode', qrcode)
    qrcode.__proto__.exportImage((path) =>{debugger;
      console.log(path)
      wx.saveImageToPhotosAlbum({
        filePath: path,
        success(res) {  
          wx.showToast({  
            title: '保存图片成功！',  
          })  
        },  
        fail(res) {  
          wx.showToast({  
            title: '保存图片失败！',  
          })  
        },  
      })
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})